#!/bin/bash

dir="$(dirname "$0")"

PURGED="$dir/data/purge.list"

# Remove unused/unwanted packages that are listed in 'data/purge.list'
function unused {
	if (eval `resize` && whiptail \
		--title "Remove Un-used Pre-installed Applications" \
		--yesno "Current list of pre-installed applications to remove: \n\n$(cat $PURGED) \n\nAre you sure you want proceed?" \
		$LINES $COLUMNS $(( $LINES - 12 )) \
		--scrolltext ) then
		show_info 'Removing unused pre-installed applications...'
		show_warning 'Requires root privileges'
		sudo apt-get remove -y $(cat $PURGED)
		# Done
		show_success 'Done.'
		whiptail --title "Finished" --msgbox "Your list of unwanted pre-installed applications have been removed." 8 78
		cleanup
	else
		cleanup
	fi
}

# Remove old kernels
function kernels {
	if (whiptail --title "Remove Outdated Kernels" --yesno "If you're not currently using the newest kernel installed on your system, the following may break it. \n\nAre you sure you want proceed?" 10 60) then
		show_info 'Removing outdated kernel(s)...'
		show_warning 'Requires root privileges'
		sudo dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | grep -v linux-libc-dev | xargs sudo apt-get -y --no-install-recommends purge
		show_success 'Done.'
		whiptail --title "Finished" --msgbox "Outdated kernels have been successfully removed." 8 78
		cleanup
	else
		cleanup
	fi
}

# Remove Orphaned Packages
function orphaned {
	show_info 'Removing orphaned packages...'
	show_warning 'Requires root privileges'
	sudo apt-get autoremove -y
	show_success 'Done.'
	whiptail --title "Finished" --msgbox "Orphaned packages have been successfully removed." 8 78
	cleanup
}

# Remove Leftover Config files
function leftovers {
	show_info 'Removing leftover configuration files...'
	show_warning 'Requires root privileges'
	sudo dpkg --purge $(COLUMNS=200 dpkg -l | grep '^rc' | tr -s ' ' | cut -d ' ' -f 2)
	show_success 'Done.'
	# Check
	whiptail --title "Finished" --msgbox "Leftover configuration files have been removed." 8 78
	cleanup
}

# Clean apt cache
function clean-cache {
	show_info 'Cleaning package cache...'
	show_warning 'Requires root privileges'
	sudo apt-get clean
	show_success 'Done.'
	whiptail --title "Finished" --msgbox "Package cache has been cleaned." 8 78
	cleanup
}

# Cleanup System
function cleanup {
	eval `resize` 
	CLEANUP=$(whiptail \
		--notags \
		--title "System Cleanup" \
		--menu "\nWhat would you like to do?" \
		--cancel-button "Go Back" \
		$LINES $COLUMNS $(( $LINES - 12 )) \
		clean-cache 'Clean package cache' \
		leftovers   'Remove leftover configuration files' \
		orphaned	'Remove orphaned packages' \
		unused	  'Remove unused pre-installed packages' \
		kernels	 'Remove old kernel(s)' \
		3>&1 1>&2 2>&3)
	 
	exitstatus=$?
	if [ $exitstatus = 0 ]; then
		$CLEANUP
	else
		main
	fi
}